package com.hcrazy.manchester.app.services;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.joda.time.DateTime;

import com.hcrazy.manchester.app.jdbc.BookFoodDAO;
import com.hcrazy.manchester.app.jdbc.CouponDAO;
import com.hcrazy.manchester.app.jdbc.LiveAddressDAO;
import com.hcrazy.manchester.app.jdbc.PraiseDAO;
import com.hcrazy.manchester.app.jdbc.RentDAO;
import com.hcrazy.manchester.app.jdbc.SecondaryDAO;
import com.hcrazy.manchester.app.jdbc.TopicDAO;
import com.hcrazy.manchester.app.pojo.ErrorMsg;
import com.hcrazy.manchester.app.pojo.Praise;
import com.hcrazy.manchester.app.utils.Constant;

public class PraiseService {

	private TopicDAO topicDAO;
	
	public TopicDAO getTopicDAO() {
		return topicDAO;
	}

	public void setTopicDAO(TopicDAO topicDAO) {
		this.topicDAO = topicDAO;
	}

	private PraiseDAO praiseDAO;
	
	public PraiseDAO getPraiseDAO() {
		return praiseDAO;
	}

	public void setPraiseDAO(PraiseDAO praiseDAO) {
		this.praiseDAO = praiseDAO;
	}
	
	private LiveAddressDAO liveAddressDAO;
	
	public LiveAddressDAO getLiveAddressDAO() {
		return liveAddressDAO;
	}

	public void setLiveAddressDAO(LiveAddressDAO liveAddressDAO) {
		this.liveAddressDAO = liveAddressDAO;
	}
	
	private BookFoodDAO bookFoodDAO;

	public BookFoodDAO getBookFoodDAO() {
		return bookFoodDAO;
	}

	public void setBookFoodDAO(BookFoodDAO bookFoodDAO) {
		this.bookFoodDAO = bookFoodDAO;
	}
	
	private CouponDAO couponDAO;

	public CouponDAO getCouponDAO() {
		return couponDAO;
	}

	public void setCouponDAO(CouponDAO couponDAO) {
		this.couponDAO = couponDAO;
	}

	private RentDAO rentDAO;
	
	public RentDAO getRentDAO() {
		return rentDAO;
	}

	public void setRentDAO(RentDAO rentDAO) {
		this.rentDAO = rentDAO;
	}
	
	private SecondaryDAO secondaryDAO;
	
	public SecondaryDAO getSecondaryDAO() {
		return secondaryDAO;
	}

	public void setSecondaryDAO(SecondaryDAO secondaryDAO) {
		this.secondaryDAO = secondaryDAO;
	}
	
	/**
	 * 点赞
	 * @param praise
	 * @return
	 * @throws Exception
	 */
	public Object addOrUpdatePraise(Praise praise) throws Exception{
		Map<String,String> result = new HashMap<String,String>();
		result.put(Constant.SINGLE_RETURN,"true");
		
		DateTime dt = new DateTime();
		praise.setPraiseTime(dt.toString("yyyy-MM-dd HH:mm:ss"));
		
		//查询该用户是否点过赞
		Praise p = praiseDAO.getPraiseByUserId(praise.getUserId(),praise.getModuleId(),praise.getModuleType());
		if(null != p){
			String lastTime = p.getPraiseTime();
			String yestoday = dt.minusDays(1).toString("yyyy-MM-dd HH:mm:ss");
			if(yestoday.compareTo(lastTime) < 0){
				return new ErrorMsg(Constant.ERROR_PRAISE_INNER_ONE_DAY,"一天之内只允许点一次赞！");
			}
		}
		
		praiseDAO.addPraise(praise);
		
		//根据模块不同分别设置模块点赞数量
		switch(praise.getModuleType()){
			case Constant.COMM_ADDRESS_INFO_MODULE:
				liveAddressDAO.addPraiseCount(praise.getModuleId());
				break;
			case Constant.COMM_BOOK_INFO_MODULE:
				bookFoodDAO.addPraiseCount(praise.getModuleId());
				break;
			case Constant.COMM_COUPON_INFO_MODULE:
				couponDAO.addPraiseCount(praise.getModuleId());
				break;
			case Constant.COMM_RENT_INFO_MODULE:
				rentDAO.addPraiseCount(praise.getModuleId());
				break;
			case Constant.COMM_SECONDARY_INFO_MODULE:
				secondaryDAO.addPraiseCount(praise.getModuleId());
				break;
			case Constant.COMM_SIMPLE_INFO_MODULE:
				
				break;
			case Constant.COMM_TICKET_INFO_MODULE:
				bookFoodDAO.addPraiseCount(praise.getModuleId());
				break;
			case Constant.COMM_BBS_INFO_MODULE:
				topicDAO.addPraiseCount(praise.getModuleId());
				break;
			default:
				break;
		}
		
		return result;
	}
	
	public Praise getInfoById(int infoId) throws Exception{
		return praiseDAO.getById(infoId);
	}
	
	public void delPraise(int infoId) throws Exception{
		praiseDAO.delPraise(infoId);
	}
	
	public List<Praise> listAllPraises(int moduleType,long moduleId) throws Exception{
		return praiseDAO.listAllPraises(moduleType, moduleId);
	}
	
	/**
	 * 获取模块被点赞数量
	 * @param moduleType
	 * @param moduleId
	 * @return
	 * @throws Exception
	 */
	public long getCountByModule(int moduleType,long moduleId) throws Exception {
		long count = 0;
		
		switch(moduleType){
			case Constant.COMM_ADDRESS_INFO_MODULE:
				count = liveAddressDAO.getPraiseCount(moduleId);
				break;
			case Constant.COMM_BOOK_INFO_MODULE:
				count = bookFoodDAO.getPraiseCount(moduleId);
				break;
			case Constant.COMM_COUPON_INFO_MODULE:
				count = couponDAO.getPraiseCount(moduleId);
				break;
			case Constant.COMM_RENT_INFO_MODULE:
				count = rentDAO.getPraiseCount(moduleId);
				break;
			case Constant.COMM_SECONDARY_INFO_MODULE:
				count = secondaryDAO.getPraiseCount(moduleId);
				break;
			case Constant.COMM_SIMPLE_INFO_MODULE:
				
				break;
			case Constant.COMM_TICKET_INFO_MODULE:
				count = bookFoodDAO.getPraiseCount(moduleId);
				break;
			default:
				break;
		}
		
		return count;
	}
}
